

fn var(){
    let a = 1_123;
    let b = a + 1_333;
    println!("a={0},b={1}",a, b);

    let mut a = 1_123;
    println!("mutable a={}", a);
    a = 234; // change mutable a 
    let b = a + 1; // rebind 

    // C is const  , rebind failed
    const C : i32 = 2345;
    println!("Hello, world!{0}---{0}--{1}--{2}", a, b, C);
}
fn tup(){
    let tup : (i32, f64, u8) = (0, 1.0, 2);
    println!("tuple.0:{0}", tup.0);

    let (x, y, z) = tup;
    println!("tuple.0:{0}, {1}, {2}", x, y, z);
}
fn arr(){
    let arr = [1, 2, 3, 4]; // arr[0] =1 compile error
    println!("array:{0}", arr[0]);

    let arr: [i32; 3] = [0, 1, 2]; // type
    println!("array:{0}", arr[2]);

    let mut arr = [1, 2, 3]; //rebind to mutable array
    arr[0] = 12;
    println!("mutable array:{0}", arr[0]);
}
fn param(x: i32, y: i32) -> i32 {
    x + y
}
fn case(){
    let number = 3;
    if number < 3 {
        println!("number < 3 =>{0}", number);
    }
    else if number > 3 {
        println!("number > 3 =>{0}", number);
    }
    else{
        println!("number = 3 => {0}", number);
    }

    let sum = if number > 3 { 3 } else { 5 };
    println!("sum = {}", sum);
}
fn while_loop(){
    let mut number = 1; // for(int i = 1; i< 4; ++i)
    while number < 4 {
        println!("number:{}", number);
        number += 1;
    }
    println!("exit while loop");

}
fn for_loop(){
    //let tup = (1, 2, 3 ,4); // error
    let arr = [1, 2, 3, 4, ];
    let mut index = 0;
    for i in arr.iter(){
        println!("for x.iter: array:{}, {}",index, i);
        index += 1;
    }

    index = 0;
    for i in 0..3 {
        println!("for 0..3, array:{}, {}",index, i);
        index += 1;
    }
}
fn loop_loop(){
    let s = ['a', 'b', 'c', '0'];
    let mut i = 0;
    loop {
        let cur = s[i];
        println!("iter:{}", cur);
        if cur == '0' {
            break;
        }
        i += 1;
    }


    i = 0;
    let location = loop {
        let ch = s[i];
        if ch == '0' {
            break i; // loop return 
        }
        i += 1;
    };
    println!("location of 0 is i:{0}, location:{1}", i, location);
}


fn string_move(){
    let s1 = String::from("hello");
    let s2 = s1;
    //  println!("s1 is lost:{}", s1); 
    println!("s2 is OK:{}", s2); 

}
fn string_clone(){
    let s1 = String::from("hello");
    let s2 = s1.clone();
    println!("s1 is cloned, s1:{}", s1); 
    println!("s2 is clone from s1, s2:{}", s2); 
}


fn owner_ship(){
    fn take_ownership(s: String){
        println!("string is:{}", s);
    }
    let s = String::from("hello");
    take_ownership(s);
    //println!("s is losted:{}",s);

    fn makes_copy(x: i32){
        println!("x is:{}", x);
    }
    let x = 5;
    makes_copy(x);
    println!("x is losted:{}",x);

    fn gives_ownership() -> String{
        String::from("hello")
    }
    let s = gives_ownership();
    println!("return_ownership:{}", s);

    fn take_and_give_back(s : String) -> String{
        s
    }
    let s2 = String::from("hello2");
    let s3 = take_and_give_back(s2); // s2 move to arg; s3 = s2
    println!(" s3 is :{}", s3);
}

fn reference(){
    let s1 = String::from("hello");
    let s2 : &String = &s1;
    println!("s1:{0}, s2:{1}", s1, s2);

    fn take_ref(s : &String) {
        println!("s: {}", s);
    }
    take_ref(&s1);
}

pub fn base() {
    var();
    tup();
    arr();
    println!("result {}", param(1, 3));
    case();
    while_loop();
    for_loop();
    loop_loop();
    string_move();
    string_clone();
    owner_ship();
    reference();
}
